package com.zhou.base.components.log.trace.aspect;

import com.xxl.job.core.context.XxlJobHelper;
import com.zhou.base.components.log.trace.filter.TraceFilter;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;

import java.util.UUID;

import static com.zhou.base.components.log.trace.constant.TraceConstant.TRACE_ID;

/**
 * @author xianfeng
 * @date 2023/11/16 17:56
 */
@Aspect
public class XxlJobAspect {

    @Pointcut("@annotation(com.xxl.job.core.handler.annotation.XxlJob)")
    public void xxlJobPointcut() {
    }

    @Around("xxlJobPointcut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Throwable throwable = null;
        Object result = null;
        String uuid = UUID.randomUUID().toString().replace("-", "");
        MDC.put(TRACE_ID, uuid);
        try {
            result = joinPoint.proceed();
        } catch (Throwable var8) {
            throwable = var8;
            throw var8;
        } finally {
            XxlJobHelper.log("任务执行完成，traceId={}", uuid);
            MDC.clear();
        }
        return result;
    }
}
